<?php

class Index extends TPage
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->Master->pageHead->Title = $this->Application->Parameters["WEBSITE_TITLE"];
		if (!$this->IsPostBack)
		{			
			if ($this->Application->User->UserName != "") 
			{
				if (Common::checkAdmin($this->Application->User))
					$this->lnkAdminPanel->Visible=true;
					
				$this->GuestPanel->Visible = false;
				$this->UserPanel->Visible = true;
			}
			
			$items = CartTempDetailRecord::finder()->findAllBysession_id($this->Session->SessionID);				
			$this->lnkShoppingCart2->Text = "My Shopping Cart (" . count($items) . ")";
			$this->lnkShoppingCart3->Text = "My Shopping Cart (" . count($items) . ")";
		}
		$this->populateLatestAdditions();
		$this->populateSliderBanners();
		$this->getPromotionProduct();
		$this->populateTestimonial();		
	}
	
	public function populateSliderBanners()
	{
        $sqlmap = $this->Application->Modules['sqlmap']->Client;
		$sql = "AND banner_type = 1 ";
		$limit = 8;
		$offset = null;
		$order = "banner_id desc";
		$items = $sqlmap->queryForList("BrowseBanner", array("ADDITIONAL_CONDITION"=>$sql,"ORDER_BY"=>$order),null,$offset,$limit);
		$this->SliderBannersList->DataSource = $items;
		$this->SliderBannersList->DataBind();
		if (count($items) <= 0)
		{
		}
	}
	
	public function populateLatestAdditions()
	{
        $sqlmap = $this->Application->Modules['sqlmap']->Client;
		$sql = "AND p.product_new_arrival = 1 ";
		$limit = 5;
		$offset = null;
		$order = "p.product_id desc";
		$items = $sqlmap->queryForList("BrowseProduct", array("ADDITIONAL_CONDITION"=>$sql,"ORDER_BY"=>$order),null,$offset,$limit);
		$this->ProductList->DataSource = $items;
		$this->ProductList->DataBind();
		if (count($items) <= 0)
		{
		}
	}
	
	public function populateTestimonial()
	{
        $sqlmap = $this->Application->Modules['sqlmap']->Client;
		$sql = null;
		$limit = 4;
		$offset = null;
		$order = "testimonial_id desc";
		$items = $sqlmap->queryForList("BrowseTestimonial", array("ADDITIONAL_CONDITION"=>$sql,"ORDER_BY"=>$order),null,$offset,$limit);
		$this->TestimonialList->DataSource = $items;
		$this->TestimonialList->DataBind();
		if (count($items) <= 0)
		{
		}
	}
	
	public function getPromotionProduct($id=0)
	{
		$criteria = new TActiveRecordCriteria;
		$criteria->Condition = "product_id = (select max(p.product_id) from tbl_product p where p.product_id > 0 and p.product_publish = 1 and p.product_best_seller = 1)";
		$items = ProductRecord::finder()->findAll($criteria);
		foreach ($items as $activeRecord) 
		{
			if ($activeRecord->ImageHotDealPath != "")
				$this->imgPromotionProductPhotoThumb->ImageUrl = $this->Request->UrlManagerModule->UrlPrefix."/useruploads/images/product/thumbs/".$activeRecord->ImageBestSellerPath;
			else
				$this->imgPromotionProductPhotoThumb->ImageUrl = $this->Request->UrlManagerModule->UrlPrefix."/useruploads/images/product/thumbs/".$activeRecord->ThumbnailPath;
			$this->imgPromotionProductPhotoThumb->NavigateUrl = $this->Service->ConstructUrl('shop.ProductDetails',array('id'=>$activeRecord->ID,'alias'=>$activeRecord->Alias));	
			$this->lblPromotionProductPrice->Text = Common::getFormattedValue($activeRecord->Price);
			$this->lnkPromotionProductDetails->NavigateUrl = $this->Service->ConstructUrl('shop.ProductDetails',array('id'=>$activeRecord->ID,'alias'=>$activeRecord->Alias));
		}
		
	}
	
	public function btnLogin_Clicked($sender, $param)
	{
		if ($this->IsValid)
		{
			$auth = $this->Application->getModule('auth');
			$mail = $this->txtLoginEmail->Text;
			$pass = $this->txtLoginPassword->Text;
			$sessionExpiredSeoncds = 0;
			
			/*if ($this->chkRemember->Checked)
				$sessionExpiredSeoncds = TPropertyValue::ensureInteger($this->Application->Parameters["USER_TIMEOUT"]);*/
			
			try
			{
				if ($auth->login($mail, $pass, $sessionExpiredSeoncds))
				{
					$url = $auth->ReturnUrl;				
					//if ($this->Application->User->Status == 0)
						//$url = $this->Service->ConstructUrl("ActivateAccount");
					if (empty($url))
						$url = $this->Service->ConstructUrl("Index");					
					$this->Response->redirect($url);
				}
				else
				{
					$this->Notice->Type = UserNoticeType::Error;
					$this->Notice->Text = $this->Application->getModule('message')->translate('USER_LOGIN_FAILED');
					$this->txtLoginPassword->Text="";
					//$this->txtLoginPassword->focus();
				}
			}
			catch (TException $e)
			{
				echo $e;
			}
		}		
	}
	
	protected function btnLogout_Clicked($sender, $param)
	{
		$this->Application->getModule("auth")->logout();
		$this->Response->redirect($this->Service->ConstructUrl("Index"));
	}
}

?>